import { defineStore } from "pinia"
import { getRoleList, addRole, updateRole } from "@/api/role"
import { RoleState } from "./types/roleType"
import type { roleInfoData, getRoleListResponseData, addRoleResponseData } from "@/api/role/type"
// 创建小仓库
// defineStore(仓库id,仓库配置对象)
let useRoleStore = defineStore("Role", {
    state: (): RoleState => {
        return {
            roles: []
        }
    },
    // 处理异步
    actions: {
        async roleList() {
            // result是一个变量，它是一个Promise对象，它的类型是getRoleListResponseData
            // await表示等待result执行完毕，然后再执行下面的代码
            let result: getRoleListResponseData = await getRoleList();
            if (result.status === 0) {
                this.roles = result.data
                return "OK"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        async useAddRole(data: roleInfoData) {
            let result: addRoleResponseData = await addRole(data);
            if (result.status === 0) {
                return "OK"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        async useUpdateRole(data: roleInfoData) {
            let result: addRoleResponseData = await updateRole(data);
            if (result.status === 0) {
                return "OK"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        }

    },
    getters: {}
})
export default useRoleStore;